#pragma once

#include "../../render/components.hpp"
#include "../../tools/grpc_interface.h"
#include "qlib/all.h"

namespace qbe::editing::Drift {

struct DriftModel {
  bool update = false;
  bool inited = false;
  bool exitable = true;
  bool request_exit = false;
  double m_drift_distance = 10.000;
  bool m_can_create = false;
  bool get_ui_data = false;
  bool is_too_large = false;
  bool is_first_ui = true;
  bool first_open = false;
  entt::entity entity_hover = entt::null;
  uint32_t layer;
  std::vector<qlib::Path::s_ptr> m_line_eles;
  std::vector<qlib::Polygon::s_ptr> m_polygon_eles;
  std::vector<qlib::Rectangle::s_ptr> m_rectangle_eles;
  std::vector<qlib::Ellipse::s_ptr> m_ellipse_eles;
  //可视化
  qbe::render::LineListMesh drift_polygon_lines;  //偏移的多边形和线
  tools::GrpcInterfaceObjcet grpc_interface;

  void updateVisualData() {
    std::vector<std::vector<qlib::Vec2d>> list;
    //测试
    // m_line_eles.clear();
    // m_polygon_eles.clear();
    // m_rectangle_eles.clear();
    // m_ellipse_eles.clear();
    // qlib::Path::s_ptr line_test = qlib::Path::create();
    // line_test->spine.points = {qlib::Vec2d(0,0), qlib::Vec2d(20,20), qlib::Vec2d(0,20), qlib::Vec2d(10,-20)};
    // qlib::Polygon::s_ptr poly_test = qlib::Polygon::create();
    // poly_test->points = {qlib::Vec2d(50,50), qlib::Vec2d(50,70), qlib::Vec2d(70,70), qlib::Vec2d(70,50)};
    // qlib::Rectangle::s_ptr rect_test = qlib::Rectangle::create();
    // rect_test->pos = qlib::Vec2d(-50,-50);
    // rect_test->width = 50;
    // rect_test->height = 100;
    // qlib::Ellipse::s_ptr ellipse_test = qlib::Ellipse::create();
    // ellipse_test->center = qlib::Vec2d(-50,-50);
    // ellipse_test->radius = qlib::Vec2d(10,40);

    // m_line_eles.push_back(line_test);
    // m_polygon_eles.push_back(poly_test);
    // m_rectangle_eles.push_back(rect_test);
    // m_ellipse_eles.push_back(ellipse_test);

    for (auto line : m_line_eles) {
      list.push_back(line->spine.points);
    }

    for (auto polygon : m_polygon_eles) {
      auto points = polygon->points;
      if (points.size() > 2)
        points.push_back(points[0]);
      list.push_back(points);
    }

    for (auto rectangle : m_rectangle_eles) {
      auto points = (rectangle->to_polygon())->points;
      if (points.size() > 2)
        points.push_back(points[0]);
      list.push_back(points);
    }

    for (auto ellipse : m_ellipse_eles) {
      auto points = (ellipse->to_polygon())->points;
      if (points.size() > 2)
        points.push_back(points[0]);
      list.push_back(points);
    }

    drift_polygon_lines = qbe::render::LineListMesh(list);
  }

  void clear() {
    m_line_eles.clear();
    m_polygon_eles.clear();
    m_rectangle_eles.clear();
    m_ellipse_eles.clear();
    clear_visual_data();
  }

  void clear_visual_data() { updateVisualData(); }
};

}  // namespace qbe::editing::Drift